<?php

class Custom_model extends CI_Model {

    function __construct() {
        parent::__construct();

        $this->tableName = "custom"; //
    }

    /*
     * 添加
     */

    public function addData($data) {
        $outArr = array();
        if (!empty($data)) {
            $outArr = $this->db->insert($this->tableName, $data);
            $outArr = $this->db->insert_id();
        }
        return $outArr;
    }

    /*
     * 编辑
     */

    public function updateData($paramArr = array()) {
        $optionArr = array(
            "fieldArr" => array(),
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        if (!empty($fieldArr)) {
            $outArr = $this->db->update($this->tableName, $fieldArr, $whereArr);
        }
        return $outArr;
    }

    /*
     * 删除
     */

    public function delData($paramArr = array()) {
        $optionArr = array(
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        $outArr = $this->db->delete($this->tableName, $whereArr);
        return $outArr;
    }

    /*
     * 得到
     */

    public function getData($where = '', $page = 1, $pageCount = 20, $orderBy = "ORDER BY id DESC", $fields = '*') {

        $result = array();
        $sql = "SELECT " . $fields . " FROM " . $this->tableName . " " . $where . " " . $orderBy;
        if ($pageCount > 0) {
            $sql .= " LIMIT " . (($page - 1) * $pageCount) . ", " . $pageCount;
        }
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    /*
     * 得到条数
     */

    function getDataCount($where = '') {
        $num = 0;
        $sql = "SELECT COUNT(*) AS num FROM " . $this->tableName . " " . $where . " LIMIT 1";
        $query = $this->db->query($sql);
        if ($query) {
            $res = $query->result_array();
            $num = $res[0]["num"];
        }
        return $num;
    }

    public function getDataById($id) {
        $outArr = array();
        $outArr = $this->getData("WHERE id=" . $id, 1, 0, "");
        if (!empty($outArr)) {
            $outArr = $outArr[0];
        }
        return $outArr;
    }

    function status() {
        return [
            1 => ["name" => "启用"],
            0 => ["name" => "禁止"],
        ];
    }

    function type() {
        return [
            1 => ["name" => "供应商"],
            2 => ["name" => "客户"],
        ];
    }

    public function needField($addParam = []) {
        $param = [
            ["field" => "type_id", "name" => "公司类型", "is_need" => 1, "type" => "z_int"],
            ["field" => "cmp_name", "name" => "公司全称", "is_need" => 1, "type" => "string"],
            ["field" => "cmp_bf", "name" => "公司简称", "is_need" => 1, "type" => "string",],
            ["field" => "contacts", "name" => "联系人", "is_need" => 0, "type" => "string",],
            ["field" => "tel", "name" => "联系电话", "is_need" => 0, "type" => "string",],
            ["field" => "email", "name" => "电子邮箱", "is_need" => 0, "type" => "string",],
            ["field" => "address", "name" => "公司地址", "is_need" => 0, "type" => "string",],
            ["field" => "remark", "name" => "备注", "is_need" => 0, "type" => "string",],
            ["field" => "status_id", "name" => "启用状态", "is_need" => 0, "type" => "int"],
        ];
        if (!empty($addParam)) {
            $param = array_merge($param, $addParam);
        }
        return $param;
    }

    function getDataSetKey() {
        $outArr = array();
        $result = $this->getData('', 1, 0, "");
        if (!empty($result)) {
            foreach ($result as $k => $v)
                $outArr[$v["id"]] = $v;
        }
        return $outArr;
    }

}
